package com.itheima.reggie.service.impl;

import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.dto.PageDto;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import com.itheima.reggie.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Emp的业务实现类
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     *
     * @param employee
     * @return
     */
    @Override
    public R login(Employee employee) {
        //判断用户名或密码是否为空
        if (!StrUtil.isAllNotBlank(employee.getUsername(), employee.getPassword())) return R.error("用户名或密码为空");

        //根据用户名查询员工Emp对象
        Employee one = this.getOne(Wrappers.lambdaQuery(Employee.class).eq(Employee::getUsername, employee.getUsername()));

        //判断用户名对应的对象是否存在
        if (ObjectUtil.isNull(one)) return R.error("用户名或密码错误");

        //存在，把用户输入的密码进行md5加密
        String md5 = SecureUtil.md5(employee.getPassword());

        //密码比对，如果不一致则返回登陆失败
        if (!StrUtil.equals(md5, one.getPassword(), true)) return R.error("用户名或密码错误");

        //判断用户的账号状态是否为禁用
        if (one.getStatus() == 0) return R.error("账户已被禁用");

        //登陆成功返回用户信息
        return R.success(one);
    }

    /**
     * 添加员工
     *
     * @param employee
     * @return
     */

    public R addEmp(Employee employee) {
       /* //参数校验
        if (employee == null) {
            return R.error("参数异常");
        }

        //设置初始密码
        String password = SecureUtil.md5("123456");
        employee.setPassword(password);
        //补全对象
        //设置值
        employee.setStatus(1);//状态为启用
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        //存入数据库
        employeeMapper.insert(employee);*/
        //判断参数
        if (ObjectUtil.isNull(employee)) return R.error("参数错误");
        if (!PhoneUtil.isMobile(employee.getPhone())) return R.error("手机号不正确");
        if (!IdcardUtil.isValidCard(employee.getIdNumber())) return R.error("身份证不正确");

        //判断账号是否存在
        Employee one = this.getOne(Wrappers.lambdaQuery(Employee.class).eq(Employee::getUsername, employee.getUsername()));
        if (ObjectUtil.isNotNull(one)) return R.error("用户名已存在");

        //补全字段值
        employee.setStatus(1);
        /*employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateUser(EmpThreadLocal.get().getId());
        employee.setCreateUser(EmpThreadLocal.get().getId());*/
        String password = SecureUtil.md5("123456");
        employee.setPassword(password);

        //保存到数据库
        this.save(employee);
        return R.success("添加成功");
    }

    /**
     * 分页查询
     *
     * @param pageDto
     * @return
     */
    @Override
    public R page(PageDto pageDto) {
        /*//1.创建分页条件
        Page pageInfo = new Page(pageDto.getPage(), pageDto.getPageSize());

        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(pageDto.getName()), Employee::getName, pageDto.getName());
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        //执行查询
        Page page = employeeMapper.selectPage(pageInfo, queryWrapper);
*/

        pageDto.check();

        //查询数据库
        Page<Employee> page = this.page(new Page<>(pageDto.getPage(), pageDto.getPageSize()),
                Wrappers.lambdaQuery(Employee.class).like(ObjectUtil.isNotNull(pageDto.getName()),
                        Employee::getName, pageDto.getName()));


        return R.success(page);
    }

    /**
     * 修改员工状态
     *
     * @param employee
     * @param flag
     * @return
     */
    @Override
    public R updateEmp(Employee employee, String flag) {
        if (StrUtil.equals(flag, "status", true)) {
            //校验是否为空
            if (!ObjectUtil.isAllNotEmpty(employee.getId(), employee.getStatus())) return R.error("参数非法");
        } else {
            //判断参数是否合法
            if (ObjectUtil.isNull(employee)) return R.error("参数错误");
            if (!PhoneUtil.isMobile(employee.getPhone())) return R.error("手机号不正确");
            if (!IdcardUtil.isValidCard(employee.getIdNumber())) return R.error("身份证不正确");
            //从业务上限制用户名不能修改
            //如果允许修改用户名，需要判断用户输入的用户名和之前是否一样
            //根据id获取原来的信息
            Employee oldId = this.getOne(Wrappers.lambdaQuery(Employee.class).eq(Employee::getId, employee.getId()));
            if (!StrUtil.equals(oldId.getUsername(), employee.getUsername())) {

                Employee one = this.getOne(Wrappers.lambdaQuery(Employee.class).eq(Employee::getUsername, employee.getUsername()));
                if (ObjectUtil.isNotNull(one)) return R.error("用户名已存在");
            }
        }
        //修改数据库中的员工状态
        employeeMapper.updateById(employee);
        if (employee.getStatus() == 0) {
            return R.success("禁用成功");
        } else {
            return R.success("启用成功");
        }

    }
}
